﻿using DeviceController.Wpf.SaveInfos;
using DeviceController.Wpf.SaveInfoServices;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DeviceController.Wpf.ViewModels.UserManageViewModels
{
    public class UserManager
    {
        private readonly ISaveInfoService _typeSaveInfoService;

        public UserManager(ISaveInfoService typeSaveInfoService)
        {
            _typeSaveInfoService = typeSaveInfoService;
        }

        public void Register(UserSaveInfo userSaveInfo)
        {
            var userInfo = _typeSaveInfoService.Read<UserInfoSaveInfo>();

            if (userInfo.Users.Any(user => user.Password == userSaveInfo.Password || user.Number == userSaveInfo.Number))
            {
                throw new InvalidOperationException("员工工号或员工卡ID已经被注册。");
            }

            userInfo.Users.Add(userSaveInfo);

            _typeSaveInfoService.Write(userInfo);
        }

        public List<UserSaveInfo> GetAll()
        {
            var userInfo = _typeSaveInfoService.Read<UserInfoSaveInfo>();

            return userInfo.Users;
        }

        public void RemoveRange(List<string> numvers)
        {
            var userInfo = _typeSaveInfoService.Read<UserInfoSaveInfo>();

            foreach (var number in numvers)
            {
                userInfo.Users.RemoveAll(user => user.Number == number);
            }

            _typeSaveInfoService.Write(userInfo);
        }
    }
}
